S=6;%number of simulations
L=15;%forecasting horizon
T=298;
for adj=0:1
for pull=2:10
%   pull is the cityid CHI TO 2, DENVER TO 3, DC CORRESPONDS TO 4, LA TO 5, PHO TO 6,
%%SANDIEGO TO 7, SAN FRAN TO 8, SEATTLE TO 9, VEGAS TO 10
% adj indexes whether it is the simple or adjusted list price index
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
%set the seed
rand('state',11345);
randn('state',11345);

if(pull==2)
    data=importdata('chi_list.txt');
elseif(pull==3)
    data=importdata('denver_list.txt');
elseif(pull==4)
    data=importdata('dc_list.txt');
elseif(pull==5)
    data=importdata('la_list.txt');
elseif(pull==6)
    data=importdata('pho_list.txt');
elseif(pull==7)
    data=importdata('sandiego_list.txt');
elseif(pull==8)
    data=importdata('sanfran_list.txt');
elseif(pull==9)
    data=importdata('seattle_list.txt');
else
    data=importdata('vegas_list.txt');
end

N=length(data)*3;weights=data(1:57,19:end);

%Set up simulations over closing-agreement date
for i=1:12
weights(:,i)=weights(:,i)./sum(weights(:,i));
end
k=zeros(N/3,S,12);
for i=1:12
for s=1:S
k(:,s,i)=randsample([-4:1:52]',N/3,true,weights(:,i));
end
end

dayl=data(:,1);dayls=zeros(length(data),S);seas=data(:,16);
for s=1:S
for i=1:length(data)
dayls(i,s)=dayl(i)+7*(k(i,s,seas(i)+1)-1);
end
end


%case shiller index-logged
%alphabetical order
csdata=importdata('cs.txt');cstemp=csdata(:,pull);csid=csdata(:,1);csfull=cstemp;

myo=data(:,2);my0o=data(:,3);salepro=data(:,4);rwo=data(:,13);rw2o=data(:,14);rw3o=data(:,15);rw3o=(adj==0)+(adj==1).*rw3o;
houseido=data(:,17); weeknumo=data(:,18);

priceom=repmat(data(:,5),1,L);
if(adj==1)
priceom=repmat(data(:,6),1,L);
end

%weights
rwo=rwo.*rw2o.*rw3o;

datao=[myo my0o salepro rwo houseido ];

weeknumot=unique(weeknumo);
%create unique weeknum identifier
for i=1:N/3    
    weeknumo(i)=find(weeknumo(i)==weeknumot);
end

%find the weeknumber of the last week in each month
wc=ones(T,1)*99999999;
for r=min(myo):298
    wc(r)=max(weeknumo(myo==r));
end

start=max(242,min(myo));
output=zeros(T-start+1,14,L);

for l=1:L
    ll=(l-1)*4+1;
%pull the adjusted price that corresponds to lag l
priceomo=priceom(:,l); 
coeff=[];coefflag1=[];coefflag2=[];coefflag3=[];coefflag4=[];coefflag5=[];coefflag6=[];coefflag7=[];coefflag8=[];coefflag9=[];coefflag10=[];coefflag11=[];coefflag12=[];coefflag13=[];

parfor r=start:T       
   
data=datao(weeknumo<(wc(r)+4-ll),:);
priceo=priceomo(weeknumo<(wc(r)+4-ll));
dayl=dayls(weeknumo<(wc(r)+4-ll),:);

myo=data(:,1);my0o=data(:,2);salepro=data(:,3);rwo=data(:,4);houseido=data(:,5);
myo=repmat(myo,1,S);my0o=repmat(my0o,1,S);salepro=repmat(salepro,1,S);priceo=repmat(priceo,1,S);rwo=repmat(rwo,1,S);houseido=repmat(houseido,1,S);

rwo=rwo-rwo.*(priceo<5);rwo=rwo-rwo.*(priceo>400);
my=myo;
%calculate simulated closing date    
for m=1:15
my=my+(dayl>31*(m) & dayl<=31*(m+1)).*m;
end
my=my-(dayl<0);
my=my-(dayl<-31);
my=reshape(my,length(my)*S,1);my0o=reshape(my0o,length(my0o)*S,1);salepro=reshape(salepro,length(salepro)*S,1);priceo=reshape(priceo,length(priceo)*S,1);rwo=reshape(rwo,length(rwo)*S,1);
fulldatao=[my my0o salepro.*rwo priceo.*rwo rwo];

N=length(rwo)*3;
temp2=[0;1;2];temp2=kron(temp2,ones(N/3,1));


% replace each observation with 2 copies to replicate CS methodology
fulldatao=repmat(fulldatao,3,1);
my=fulldatao(:,1);my0=fulldatao(:,2);salepr=fulldatao(:,3);price=fulldatao(:,4);rw=fulldatao(:,5);

N=length(rw);

%update month numbers
my=my+temp2;my0=my0+temp2;
delta0=csfull(my0);
yv=(salepr./exp(delta0));


%form the X and Z matrices in case shiller; 
X=zeros(N,r-(start-1));
for i=1:r-(start-1)
    X(:,i)=price.*(my==(start-1+i));    
end
Z=(X>0).*repmat(rw,1,r-(start-1));

A=Z'*X;B=Z'*yv;
temp=(1./(A\B));
coeff=[coeff;temp(r-(start-1))];
coefflag1=[coefflag1;temp(max(1,r-(start-1)-1))];
coefflag2=[coefflag2;temp(max(1,r-(start-1)-2))];
coefflag3=[coefflag3;temp(max(1,r-(start-1)-3))];
coefflag4=[coefflag4;temp(max(1,r-(start-1)-4))];
coefflag5=[coefflag5;temp(max(1,r-(start-1)-5))];
coefflag6=[coefflag6;temp(max(1,r-(start-1)-6))];
coefflag7=[coefflag7;temp(max(1,r-(start-1)-7))];
coefflag8=[coefflag8;temp(max(1,r-(start-1)-8))];
coefflag9=[coefflag9;temp(max(1,r-(start-1)-9))];
coefflag10=[coefflag10;temp(max(1,r-(start-1)-10))];
coefflag11=[coefflag11;temp(max(1,r-(start-1)-11))];
coefflag12=[coefflag12;temp(max(1,r-(start-1)-12))];
coefflag13=[coefflag13;temp(max(1,r-(start-1)-13))];
end


output(:,:,l)=log([coeff,coefflag1,coefflag2,coefflag3,coefflag4,coefflag5,coefflag6,coefflag7,coefflag8,coefflag9,coefflag10,coefflag11,coefflag12,coefflag13]);
end

out=[];
for l=1:L
out=[out;output(:,:,l),ones(size(output,1),1)*((l-1)*4+1),[start:1:T]'];
end

out(isnan(out))=-99;

if(pull==2&&adj==1)
  save chifore.txt out -ascii -double -tabs 
elseif(pull==3&&adj==1)
     save denverfore.txt out -ascii -double -tabs 
elseif(pull==4&&adj==1)
    save dcfore.txt out -ascii -double -tabs 
elseif(pull==5&&adj==1)
    save lafore.txt out -ascii -double -tabs 
elseif(pull==6&&adj==1)
      save phofore.txt out -ascii -double -tabs 
elseif(pull==7&&adj==1)
     save sandiegofore.txt out -ascii -double -tabs 
elseif(pull==8&&adj==1)
      save sanfranfore.txt out -ascii -double -tabs 
elseif(pull==9&&adj==1)
      save seattlefore.txt out -ascii -double -tabs 
elseif(pull==10&&adj==1)
      save vegasfore.txt out -ascii -double -tabs 
elseif(pull==2&&adj==0)
  save chiforeua.txt out -ascii -double -tabs 
elseif(pull==3&&adj==0)
     save denverforeua.txt out -ascii -double -tabs 
elseif(pull==4&&adj==0)
    save dcforeua.txt out -ascii -double -tabs 
elseif(pull==5&&adj==0)
    save laforeua.txt out -ascii -double -tabs 
elseif(pull==6&&adj==0)
      save phoforeua.txt out -ascii -double -tabs 
elseif(pull==7&&adj==0)
     save sandiegoforeua.txt out -ascii -double -tabs 
elseif(pull==8&&adj==0)
      save sanfranforeua.txt out -ascii -double -tabs 
elseif(pull==9&&adj==0)
      save seattleforeua.txt out -ascii -double -tabs 
else
      save vegasforeua.txt out -ascii -double -tabs 

end
end
end


